﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using QLNhaThuoc.DTO;

namespace QLNhaThuoc.DAL
{
    class HoaDonDA:DataProvider
    {
        private CTHoaDonDA cthdDA = new CTHoaDonDA();

        public HoaDonDA()
        {
            connect();
            fillMyTable("HOA_DON");
        }

        public void insert(HoaDon info)
        {
            DataRow row = myTable.NewRow();
            //row["MaHD"] = info.MaHD;
            row["MaNV"] = info.MaNV;
            row["NgayXuat"] = info.NgayXuat;
            row["TenNguoiMua"] = info.TenNguoiMua;
            row["DiaChiNguoiMua"] = info.DiaChiNguoiMua;
            row["ThanhTien"] = info.ThanhTien;
            row["TheoDon"] = info.TheoDon;
            myTable.Rows.Add(row);

            updateMyTable();

            foreach (CTPhieu cthd in info.ctHoaDon)
            {
                cthd.Ma = int.Parse(row["MaHD"].ToString());
                cthdDA.insert(cthd);
            }
        }

        public void delete(HoaDon info)
        {
            foreach (CTPhieu cthd in info.ctHoaDon)
            {
                cthdDA.delete(cthd);
            }

            myTable.Rows.Find(info.MaHD).Delete();

            updateMyTable();           
        }

        public void update(HoaDon info)
        {
            DataRow row = myTable.Rows.Find(info.MaHD);
            //row["MaHD"] = info.MaHD;
            row["MaNV"] = info.MaNV;
            row["NgayXuat"] = info.NgayXuat;
            row["TenNguoiMua"] = info.TenNguoiMua;
            row["DiaChiNguoiMua"] = info.DiaChiNguoiMua;
            row["ThanhTien"] = info.ThanhTien;
            row["TheoDon"] = info.TheoDon;

            foreach (CTPhieu cthd in info.ctHoaDon)
            {
                cthdDA.update(cthd);
            }

            updateMyTable();
        }

        public bool KiemTraPK(HoaDon info)
        {
            DataRow row = myTable.Rows.Find(info.MaHD);
            if (row != null)
                return true;
            return false;
        }

        public HoaDon[] search(string expr)
        {
            string sqlString = "select * from HOA_DON where " + expr;
            ArrayList list = DongHopDuLieuTuCustomDatatable(sqlString);
            int n = list.Count;
            HoaDon[] kq = new HoaDon[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (HoaDon)list[i];
            }

            return kq;
        }

        public HoaDon[] LayListHoaDon()
        {
            ArrayList list = DongHopDuLieuTuDataTable();
            int n = list.Count;
            HoaDon[] kq = new HoaDon[n];

            for (int i = 0; i < n; i++)
            {
                kq[i] = (HoaDon)list[i];
            }

            return kq;
        }

        public override object ConvertOneRowIntoDTO(DataTable table, int i)
        {
            HoaDon obj = new HoaDon();
            obj.MaHD = int.Parse(table.Rows[i]["MaHD"].ToString());
            obj.MaNV = table.Rows[i]["MaNV"].ToString();
            obj.NgayXuat = DateTime.Parse(table.Rows[i]["NgayXuat"].ToString());
            obj.TenNguoiMua = table.Rows[i]["TenNguoiMua"].ToString();
            obj.DiaChiNguoiMua = table.Rows[i]["DiaChiNguoiMua"].ToString();
            obj.ThanhTien = int.Parse(table.Rows[i]["ThanhTien"].ToString());
            obj.TheoDon = bool.Parse(table.Rows[i]["TheoDon"].ToString());
            obj.ctHoaDon = cthdDA.LayListCTPhieu(obj.MaHD);

            return obj;
        }
    }
}
